package cc.vicp.djx314.starfood.web.dish;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.vicp.djx314.starfood.service.dishes.DishByOneService;
import cc.vicp.djx314.starfood.service.exception.DishNotFoundException;

@Controller
public class DishImageController {
	//~属性=====================================================================
	public static final Logger logger = LoggerFactory.getLogger(DishImageController.class);

	@Autowired
	private DishByOneService dishByOneService;

	@RequestMapping(value = "/dish/image/{id}.jpg", method = RequestMethod.GET)
	@ResponseBody
	public byte[] getDishImage(@PathVariable("id") Integer id)
			throws DishNotFoundException, IOException {
		try {
			Resource dishImg = dishByOneService.getDishImage(id);
			return FileCopyUtils.copyToByteArray(dishImg.getInputStream());
		} catch (IOException e2) {
			logger.error("io流发生未知错误", e2);
			throw e2;
		}
	}
}
